//
//  TMGGoldStoreViewController.m
//  TMGold
//
//  Created by Mac on 2018/4/17.
//  Copyright © 2018年 tangmu. All rights reserved.
//

#import "TMGGoldStoreViewController.h"
#import "TMGGoldStoreCollectionViewCell.h"
#import "UICollectionView+STNoresult.h"
#import "TMGStoreDetailViewController.h"
#import "TMGStoreModel.h"
@interface TMGGoldStoreViewController ()<UICollectionViewDataSource,UISearchControllerDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) UICollectionView                     *collectionView;/**< collectionView */
@property(nonatomic, strong) NSMutableArray<TMGStoreModel*>                   *dataSouce;/**< 数据源 */
@end

@implementation TMGGoldStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商品列表";
    
    UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
    flow.minimumLineSpacing = 5;
    flow.minimumInteritemSpacing = 5;
    flow.sectionInset = UIEdgeInsetsMake(10, 15, 5, 15);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc] initWithFrame:UIScreenFrame collectionViewLayout:flow];
    self.collectionView.backgroundColor = TM_backgroundColor;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[TMGGoldStoreCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无商品" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendGoldListRequest];
    }];
    [self.collectionView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}

#pragma --mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(photoItemWith, [TMGGoldStoreCollectionViewCell cellHeight]);
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TMGGoldStoreCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    item.model = self.dataSouce[indexPath.row];
    return item;
}
#pragma --mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TMGStoreDetailViewController * vc =  [TMGStoreDetailViewController new];
    vc.model = self.dataSouce[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendGoldListRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/shopsGoods"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"][@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 for (NSDictionary * modelDic in array) {
                                                     TMGStoreModel * model = [TMGStoreModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.collectionView reloadData];
                                                 [self.collectionView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.collectionView.mj_header endRefreshing];
                                             }];
}
@end
